/*
 * 1.6
 * Given an image represented by an NxN matrix, where each pixel in the image is 4
 * bytes, write a method to rotate the image by 90 degrees. Can you do this in place?
 */
#include <stdio.h>

// XXX: Use template so that you can access values of a matrix using subscript.
// Otherwise, the size is not passed by an (int**) declaration.
template <int n>
void rotate_matrix(int (&m)[n][n])
{
    for (int i = 0; i < n/2; i++) {
        for (int j = i; j < n-1-i; j++) {
            int temp = m[i][j];
            m[i][j] = m[j][n-1-i];
            m[j][n-1-i] = m[n-1-i][n-1-j];
            m[n-1-i][n-1-j] = m[n-1-j][i];
            m[n-1-j][i] = temp;
        }
    }
}

template <int n>
void print_matrix(int (&m)[n][n])
{
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            printf("%d ", m[i][j]);
        }
        printf("\n");
    }
}

int main()
{
    int m[1][1] = { { 1 } };
    rotate_matrix(m);
    print_matrix(m);
    int m2[2][2] = { { 1, 2 }, { 3, 4} };
    rotate_matrix(m2);
    print_matrix(m2);
    int m3[3][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
    rotate_matrix(m3);
    print_matrix(m3);
}
